<?php

include "../lib/start.php";

check_session();
check_permission(ADM_PERM);

$sel_classi = "SELECT id_classe, tempo_prolungato FROM classi ORDER BY id_classe";
try{
	$res_classi = $db->executeQuery($sel_classi);
} catch (MySQLException $ex){
	print "ko;".$ex->getQuery().";".$ex->getMessage();
	exit;
}

$sel_anno = "SELECT * FROM anni ORDER BY id_anno DESC LIMIT 1";
$res_anno = $db->executeQuery($sel_anno);
$year = new AnnoScolastico($res_anno->fetch_assoc());
$anno = $year->get_ID();

$giorni = array('Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab');
$max_ore = 8;
while($classe = $res_classi->fetch_assoc()){
	if($classe['tempo_prolungato'] != 1)
		$max = 5;
	else
		$max = $max_ore;
	foreach($giorni as $giorno){
		for($i = 0; $i < $max; $i++){
			$ins = "INSERT INTO orario (giorno, ora, classe, anno) VALUES ('$giorno', ".($i + 1).", ".$classe['id_classe'].", $anno)";
			try{
				$r_ins = $db->executeUpdate($ins);
			} catch (MySQLException $ex){
	        	print "ko;".$ex->getQuery().";".$ex->getMessage();
				exit;
	        }
		}
	}
}

print "ok;";
exit;

?>